"use strict";
var sid = Date.now();
var cache = {};
function session(req, res, next) {
    Object.defineProperty(req, "session", {
        get: function () {
            return cache[this.sessionId];
        },
        set: function (value) {
            cache[this.sessionId] = value;
        }
    });
    if (!(req.headers.cookie && (req.sessionId = parse(req.headers.cookie).sessionId))) {
        req.session = sid += 1;
        res.setHeader("Set-Cookie", ["sessionId=" + req.sessionId]);
    }
    next();
}

function parse(str) {
    var arr = str.split(";");
    var obj = {};
    arr.forEach(function (field) {
        var o = field.split("=");
        obj[o[0].trim()] = o[1];
    });

    return obj;
}

module.exports = session;

